package com.hamizhong.iflowers.web.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hamizhong.iflowers.service.ICityService;
import com.hamizhong.iflowers.service.IProvinceService;
import com.hamizhong.iflowers.utils.MyConstants;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.base.MyBaseAjaxAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.CityVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;

public class AjaxQueryCityListByProvinceIdAction extends MyBaseAjaxAction {
	private IProvinceService iProvinceService = null;
	private ICityService iCityService = null;
	
	public IProvinceService getiProvinceService() {
		return iProvinceService;
	}
	public void setiProvinceService(IProvinceService iProvinceService) {
		this.iProvinceService = iProvinceService;
	}
	public ICityService getiCityService() {
		return iCityService;
	}
	public void setiCityService(ICityService iCityService) {
		this.iCityService = iCityService;
	}

	@Override
	protected JSONObject doRequest() {
		logger.info("...invoke...AjaxQueryCityListByProvinceIdAction...");			
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		JSONObject jobj = null;
		try {
			String selectProvinceId = getRQ().getParameter("selectProvinceId");
			IOPResultDto<IObjectVO> dto = iCityService.findByProvinceId(selectProvinceId);
			List<CityVO> cityVOList = dto.getFirstResult().getCityVOList();

			for (CityVO cityVO : cityVOList) {
				jobj = new JSONObject();
				jobj.put("cityId", cityVO.getCityId());
				jobj.put("cityName", cityVO.getCityName());
				ja.add(jobj);
			}
			jo.put("data", ja.toString());
			jo.put("result", MyConstants.AJAX_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			jo.put("msg", MyErrorMsgConstants.FAILED_TO_QUERY_CITY_BY_PROVINCE);
			jo.put("result", MyConstants.AJAX_FAILED_ALERT);
		}
		return jo;
	}

}
